package org.project.product.android.pfe.fanta.adapter;

import java.util.Collections;
import java.util.List;

import org.project.product.android.pfe.fanta.domain.Category;
import org.project.product.android.pfe.fanta.view.CategoryItemView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class CategoryListAdapter extends BaseAdapter {

	private final List<Category> categories;

	public CategoryListAdapter(List<Category> categories) {
		super();
		Collections.sort(categories);
		this.categories = categories;
	}

	@Override
	public int getCount() {
		return categories.size();
	}

	@Override
	public Object getItem(int position) {
		return categories.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Category category = categories.get(position);
		if (convertView != null && convertView instanceof CategoryItemView) {
			CategoryItemView itemView = (CategoryItemView) convertView;
			itemView.reuse(category);
			return itemView;
		}
		return new CategoryItemView(parent.getContext(), category);
	}

}
